package cmd

import (
	"io/ioutil"
	"os"
	"os/exec"
	"testing"

	config "code.alibaba-inc.com/agit/quarantine-helper/config"
	"github.com/stretchr/testify/assert"
)

func TestFilterCommitsByRepository(t *testing.T) {
	var (
		assert = assert.New(t)
	)
	repoPath := "/tmp/quarantine-helperhelperhelperhelperhelper.git"
	err := os.Mkdir(repoPath, os.ModePerm)
	if err != nil {
		panic(err)
	}
	defer func(dir string) {
		os.RemoveAll(dir)
	}(repoPath)
	cmdArgs := []string{
		"git",
		"clone",
		"--bare",
		"http://gitlab.alibaba-inc.com/agit/quarantine-helper.git",
		repoPath,
	}
	cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
	err = cmd.Run()
	assert.Nil(err)
	config.Repository = repoPath
	type testCase struct {
		commitIDArray []string
		commitMap     map[string]bool
	}
	testCases := []testCase{
		testCase{
			commitIDArray: []string{
				"563a240ca7ceaf3eb26c66dfcdf8aff8e7931d98",
				"07219ee7be6868ab1f2e2f6091016f1f79708f91",
				"f900c806ce8b8f81ca6c8430becf45db7f95bce2", //
				"28f97468b2a5856061d9c3b7ad8b593192ade900",
				"3cbfc9cbff63da05f097ee1ad9eae26a4abe13c6", //
			},
			commitMap: map[string]bool{
				"563a240ca7ceaf3eb26c66dfcdf8aff8e7931d98": true,
				"07219ee7be6868ab1f2e2f6091016f1f79708f91": true,
				"28f97468b2a5856061d9c3b7ad8b593192ade900": true,
			},
		},
		testCase{
			commitIDArray: []string{
				"e709bcb694494c6ed71819b230bcadbce0448805",
				"1ebd0f8f60f762ea76d1c041b56475b0ea8dbbc7", //
				"b61195818cd1d89e4b18bf8f78ed408cd14ec151",
				"80a2f37413841e3a1989e59eda342f4bcc19a880", //
			},
			commitMap: map[string]bool{
				"e709bcb694494c6ed71819b230bcadbce0448805": true,
				"b61195818cd1d89e4b18bf8f78ed408cd14ec151": true,
			},
		},
	}

	for _, testCase := range testCases {
		output, err := filterCommitsByRepository(testCase.commitIDArray)
		assert.Nil(err)
		assert.Equal(testCase.commitMap, output)
	}
}

func TestFilterCommitsByBranchTip(t *testing.T) {
	var (
		assert = assert.New(t)
	)
	repoPath := "/tmp/git-testQueue.git"
	err := os.Mkdir(repoPath, os.ModePerm)
	if err != nil {
		panic(err)
	}
	defer func(dir string) {
		os.RemoveAll(dir)
	}(repoPath)
	cmdArgs := []string{
		"git",
		"clone",
		"--bare",
		"http://gitlab.alibaba-inc.com/julien.zpf/testQueue.git",
		repoPath,
	}
	cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
	err = cmd.Run()
	assert.Nil(err)
	config.Repository = "/tmp/git-testQueue.git"
	config.FromWeb = true

	type testCase struct {
		cimmitMap  map[string]bool
		outCommits string
		branchTip  string
	}
	testCases := []testCase{
		testCase{
			cimmitMap: map[string]bool{
				"4608783bd0f6e7ee0e71381c01bdf49f273cc43a": true,
				"5f6c6b20bafe61c0c2028b183919ccc5e73c60a8": true,
				"f346c649568d7bb239e116a91e25745dd116d388": true,
				"2518a0b8220f43bd9edf2585820c3f0f2e0ab7e1": true,
				"ebe7c0cfe771301eeb7560dbc04406d5ff11841a": true,
				"d8e45fe69cb6f48a62dd7db041bb0bada0a42041": true,
				"7fbf9e1ccb6874641b02bf1e2d3c21992d5eaa66": true,
				"723d0430dfcd80723c7441d20d3c639beeaba773": true,
			},
			outCommits: "5f6c6b20bafe61c0c2028b183919ccc5e73c60a8\n" +
				"2518a0b8220f43bd9edf2585820c3f0f2e0ab7e1\n" +
				"f346c649568d7bb239e116a91e25745dd116d388\n" +
				"ebe7c0cfe771301eeb7560dbc04406d5ff11841a\n" +
				"4608783bd0f6e7ee0e71381c01bdf49f273cc43a\n" +
				"d8e45fe69cb6f48a62dd7db041bb0bada0a42041\n" +
				"7fbf9e1ccb6874641b02bf1e2d3c21992d5eaa66\n" +
				"723d0430dfcd80723c7441d20d3c639beeaba773\n",
			branchTip: "5f6c6b20bafe61c0c2028b183919ccc5e73c60a8",
		},
	}
	for _, testCase := range testCases {
		stdout := os.Stdout
		reader, writer, _ := os.Pipe()
		os.Stdout = writer

		err := filterCommitsByBranchTip(testCase.cimmitMap, testCase.branchTip)
		assert.Nil(err)
		writer.Close()

		rawOutput, err := ioutil.ReadAll(reader)
		output := string(rawOutput)
		os.Stdout = stdout

		assert.Equal(testCase.outCommits, output)
	}
}
